package com.healthcare.common.handler;


import com.healthcare.common.constants.ResultCodeEnum;
import com.healthcare.common.exception.KetaiException;
import com.healthcare.common.util.ExceptionUtil;
import com.healthcare.common.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 作者：LSH
 * 日期：2020/1/7 19:15
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class) //所有异常
    @ResponseBody
    public Result error(Exception e){
        log.error(ExceptionUtil.getMessage(e));
       // e.printStackTrace();
        return Result.error();
    }

    @ExceptionHandler(BadSqlGrammarException.class) //指定异常
    @ResponseBody
    public Result error(BadSqlGrammarException e){
        //e.printStackTrace();
        log.error(ExceptionUtil.getMessage(e));
        return Result.setResult(ResultCodeEnum.BAD_SQL_GRAMMAR);
    }

    @ExceptionHandler(KetaiException.class)  //自定义异常
    @ResponseBody
    public Result error(KetaiException e){
        log.error(ExceptionUtil.getMessage(e));
     //   e.printStackTrace();
        return Result.error().message(e.getMessage()).code(e.getCode());
    }
}
